<?php
class goodsPoster extends Kernel {

    public $menu = array(
        '系统设置' => 'setting',
        /* '供应商管理' => 'supplier',*/
        '供应商管理' => array( 'supplier' => '供应商管理', 'contact' => '联系人查询', 'sales' => '供货查询' ),
        /*'顾客管理' => 'customer',*/
        '顾客管理' => array( 'buyer' => '信息编辑', 'shopping' => '购买查询', 'comment' => '评价查询' ),
        /*  '网络销售商' => 'vendor',
        '网络销售商' =>array('CorpSale'=>'信息编辑','CorpSale_Contact'=>'购买查询','CoprSale_shopping'=>'评价查询'),*/
        '运营管理' => array( 'stock' => '库存管理', 'order' => '订单管理', 'product' => '商品管理', 'commonInfo' => '公共信息', 'infoPromotion' => '信息推广' ),
        '市场信息' => array( 'hotSale' => '市场热卖','recommand'=>'商家推荐' ),
        '统计管理' => 'statistic',
        '促销管理' => 'promotion',
        '员工管理' => array( 'user' => '用户管理', 'userGroup' => '部门管理' ),
        '其他管理' => array( 'log' => '日志管理', 'database' => '数据库管理' )
    );
    public $admin = null;

    public function __construct() {
        parent::__construct();
        $this->t->init( WEBROOT . "themes/goodsPoster/", WEBTPL . "goodsPoster/" );
        $this->isLogin();
        $this->getmenu();
        $this->themesURL = $this->setting[ 'weburl' ] . "/themes/goodsPoster";
    }

    public function get(){
        array_shift( $_GET );
        return $_GET;
    }

    public function getchild( $arr ) {
        foreach ( $arr as $v ) {
            $tmp[ ] = $v[ 'url' ];
        }
        return $tmp;
    }

    public function getname( $c ) {
        $menu = $this->Cache()->get( 'menu' );
        foreach ( $menu as $v ) {
            if ( $v[ 'url' ] === $c ) {
                return $v[ 'title' ];
            }
        }
    }

    private function getmenu() {

        // $menu = $this->Cache()->get( 'menu','menu_supplier' );      //系统管理员用户
        $this->table = 'menu_goodsposter';
        $menu = $this->findAll();
        $tmp = array();
        foreach ( $menu as $v ) {
            $tmp[ $v[ 'id' ] ] = $v;
        }


        $menu =$this->Cache()->Access( 'set', $this->Cache()->getFile( $this->table ), $tmp );
        $menu = $this->Node()->arraySortByKey( $menu, 'oid' );
        $menu = $this->Node()->dataTree( $menu );
        $this->menu = $menu;




    }

    public function error( $msg = null, $time = 5 ) {
        $msg = $msg == null ? "请求参数错误!" . getRequest( 'QUERY_STRING' ) : $msg;
        $url = getRequest( 'HTTP_REFERER' ) ? getRequest( 'HTTP_REFERER' ) : getRequest( 'REQUEST_URI' );
        include $this->display( 'error' );
        exit;
    }

    # 返回附件上传类型
    public function getType() {
        $type = $this->setting[ 'uptype' ];
        if ( empty( $type ) ) {
            $type = "*.*";
        }
        return $type;
    }

    # 返回附件上传限制
    public function getSize() {
        if ( !empty( $this->setting[ 'upsize' ] ) ) {
            $size = $this->setting[ 'upsize' ];
        } else {
            $size = intval( ini_get( 'upload_max_filesize' ) );
            $size = $size > 5 ? $size - 2 : $size;
        }
        $size .= "MB";
        return $size;
    }

    # 检查是否登录
    private function isLogin() {
        # 后台上传文件不判断是否登陆
        if ( G( 'a' ) == 'up' ) {
            return;
        }

        if ( !$cookie = $_COOKIE[ WEBCOOKIE ] ) {
            $this->login();
            return;
        }

        $cookie = md6( $cookie, 'decode' );
        $cookie = explode( '|', $cookie );

        list( $uname, $uid, $expire ) = $cookie;

        # 检查cookie
        if ( !empty( $expire ) && $expire < time() ) {
            $this->login();
            return;
        }

        # 检查用户
        $this->table = 'user';
        if ( !$result = $this->find( array( 'id' => $uid, 'author' => $uname ) ) ) {
            $this->login();
            return;
        }

        # 检查用户状态
        if ( $result[ 'status' ] ) {
            $this->login();
            return;
        }



        unset( $result[ 'pass' ] );
        $this->admin = $result;
    }

    private function login() {
        if ( G( 'c' ) == 'login' && G( 'a' ) == 'logout' ) {
            header( "location:?c=login" );
            exit;
        }
        if ( G( 'c' ) != 'login' ) {
            header( "location:?c=login&referer=" . urlencode( getRequest( 'REQUEST_URI' ) ) );
            exit;
        }
    }
}